<?php


namespace App\Api\Repositories;


use App\Api\Models\CmcShops;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Redis;

class GeoRepositorie
{
    public static function getinfo($addr,$uid)
    {
        //获取经纬度
        $url = sprintf(config('addr'),$addr);
        $client = new Client(['timeout' => 5,'verify' => false]);
        $response = $client->get($url);
        $json = (string)$response->getBody();
        $address['lng'] = (float)substr(explode('lng":',$json)[1],0,10);
        $address['lat'] = (float)substr(explode('lat":',$json)[1],0,10);
        //存入Redis
        Redis::rawCommand('geoadd', 'citys', $address['lng'], $address['lat'], 'User'.$uid);
        //查找店铺的信息
        $shop = CmcShops::getAll();
        $long = [];
        if(is_array($shop)){
            foreach ($shop as $item){
                Redis::rawCommand('geoadd', 'citys', $item['lat'], $item['lon'], $item['shop_name']);
                $long[] = [$item['shop_name'].'-'.'User'.$uid,(int)round(Redis::rawCommand('geodist', 'citys', $item['shop_name'], 'User'.$uid),-1)];
            }
            $meter = array();
            foreach ($long as $v) {
                $meter[] = $v[1];
            }
            array_multisort($meter, SORT_ASC, $long);
            return $long;
        }else{
            return false;
        }
    }
}